<template>
    <div id="echarLine">

    </div>
</template>

<script>
    import echarts from "echarts";
    import {reactive, toRefs, onMounted, ref, watch} from "vue";

    export default {
        props: {
            colors: {
                type: Array,
                default: () => ['#39ffdc', '#d3ff68', '#89FFCE', '#FF8C68', '#FFDBEF']
            },
            echarName: {
                type: String,
                default: '访问来源'
            },
            xAxis: {
                type: Array,
                default: () => ['周一', '周二', '周三', '周四', '周五', '周六', '周日']
            },
            dataSource: {
                type: Array,
                default: () => [
                    {
                        name: '邮件营销',
                        data: [120, 132, 101, 134, 90, 230, 210]
                    },
                    {
                        name: '联盟广告',
                        data: [220, 182, 191, 234, 290, 330, 310]
                    },
                    {
                        name: '视频广告',
                        data: [150, 232, 201, 154, 190, 330, 410]
                    },
                    {
                        name: '直接访问',
                        data: [320, 332, 301, 334, 390, 330, 320]
                    },
                    {
                        name: '搜索引擎',
                        data: [820, 932, 901, 934, 1290, 1330, 1320]
                    }
                ]
            },
        },
        computed: {
            option() {
                let {xAxis, dataSource, echarName: name, colors} = this;

                return {
                    title: {
                        text: name,
                        top: 'top',
                        left: 'center',
                        textStyle: {
                            color: '#fff',
                            fontSize: 10
                        }
                    },
                    tooltip: {
                        trigger: 'axis'
                    },
                    legend: {
                        show: true,
                        type: 'scroll',
                        textStyle: {
                            color: '#fff'
                        },
                        pageTextStyle: {
                            color: '#fff'
                        },
                        show: true,
                        legendLayout: 'horizontal',
                        left: 'center',
                        top: 'bottom',
                    },
                    grid: {
                        top: '20%',
                        left: '5%',
                        right: '10%',
                        bottom: '20%',
                        containLabel: true
                    },
                    xAxis: {
                        type: 'category',
                        boundaryGap: false,
                        axisLine: {
                            lineStyle: {
                                color: '#fff'
                            }
                        },
                        data: xAxis
                    },
                    yAxis: {
                        type: 'value',
                        axisLine: {
                            lineStyle: {
                                color: '#fff'
                            }
                        },
                    },
                    series: dataSource.map((item, index) => {
                        item.type = 'line';
                        let itemStyle = {
                            normal: {
                                lineStyle: {
                                    color: colors[index] === undefined ? '#39ffdc' : colors[index]
                                }
                            }
                        }
                        item.itemStyle = itemStyle
                        return item;
                    })
                }
            }
        },
        methods: {
            drawChart() {
                let {option} = this;
                let elementById = document.getElementById("echarLine");
                let pieChart = echarts.init(elementById);
                pieChart.setOption(option);
            }
        },
        mounted() {
            let {drawChart} = this
            drawChart()
        }
    }
</script>

<style scoped>
    #echarLine {
        height: 100%;
        width: 100%;
    }
</style>
